package com.example.ad_project;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;

import com.nus.iss.ad.dbhelper.DelegateDBHelper;
import com.nus.iss.ad.model.Employee;
import com.nus.iss.ad.utils.MyUtilities;

public class DelegateActivity extends BaseActivity implements OnClickListener{
	
	
	DelegateDBHelper data=new DelegateDBHelper();
	DatePicker datePickerStartDate,datePickerEndDate;
	Spinner spinnerEmpName;
	List<Employee> empList;
	Button buttonAssignDelegate;
	Button buttonRemoveDelegate;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delegate);
		
		buttonAssignDelegate=(Button)findViewById(R.id.buttonDelegate);
		buttonAssignDelegate.setOnClickListener(this);
		
		buttonRemoveDelegate=(Button)findViewById(R.id.buttonRemoveDelegate);
		buttonRemoveDelegate.setOnClickListener(this);
		//buttonRemoveDelegate.setEnabled(LoginUserInfo.currentUser.isDeptDelegate());
		
		datePickerStartDate=(DatePicker)findViewById(R.id.datePickerStartDate);
		datePickerEndDate=(DatePicker)findViewById(R.id.datePickerEndDate);
		
		spinnerEmpName=(Spinner)findViewById(R.id.spinnerEmployeesInDept);
		empList=data.getAllEmployeeInDepartment();
		
		String[] empNameArr=new String[empList.size()];
		for(int i=0;i<empNameArr.length;i++)
		{
			Employee e=empList.get(i);
			empNameArr[i]=e.getName();
		}
		ArrayAdapter<String> adapter = 
		        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, empNameArr);

		spinnerEmpName.setAdapter(adapter);
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		if(v==buttonAssignDelegate){
		Calendar startC=Calendar.getInstance();
		startC.set(datePickerStartDate.getYear(),
				datePickerStartDate.getMonth(),
				datePickerStartDate.getDayOfMonth());
		Date startDate=startC.getTime();
		
		
		Calendar endC=Calendar.getInstance();
		endC.set(datePickerEndDate.getYear(),
				datePickerEndDate.getMonth(),
				datePickerEndDate.getDayOfMonth());
		Date endDate=endC.getTime();
		
		Date today=Calendar.getInstance().getTime();
		if(startDate.before(today)){
			showDialog("Start Date must not be before today.",false);
			return;
		}
		
		if(startDate.after(endDate)){
			showDialog("Start Date must not be after End Date.", false);
			return;
		}
		
		Employee selectedEmp=empList.get(spinnerEmpName.getSelectedItemPosition());
		
		data.addDelegate(startDate, endDate, selectedEmp.getLoginId());
		
		String msg=String.format(
				"Delegated successfully to [%s] from %s to %s", 
				selectedEmp.getName(),
				MyUtilities.dateToString(startDate, "dd-MMM-yyyy"),
				MyUtilities.dateToString(endDate, "dd-MMM-yyyy")
				);
		showDialog(msg, true);
		
		}else if(v==buttonRemoveDelegate){
			
			data.removeDelegate();
			
			String msg=String.format(
					"Removed delegate successfully."
					);
			showDialog(msg, true);
		}
		
		
		//setResult(RESULT_OK);
        //finish();
		
	}
	
	

}
